<?php
//地址控制器
class Controller_App_Address extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/address/list";
        parent::__construct($req, $res);
    }

    // 地址默认入口
    public function action_index ()
    {
        $this->action_list();
    }

    // 地址列表
    public function action_list ()
    {
        $addrList = App_Address::infoList($this->_user);
        $this->template->set_filename('smarty:app/address/list');
        $this->template->set('addressList', $addrList);
        $this->template->set('refer', $this->request->referrer());
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '我的地址', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }
	//添加地址
    public function action_add ()
    {
        if (empty($this->_post))
        {
            $this->template->set_filename('smarty:app/address/edit');
            $this->template->set('optionsProvice', App_Region::optionsByParentId());
            $this->template->set('refer', $this->request->referrer());
        }
        else
        {
            $refer = Arr::get($this->_post, 'refer');
            App_Address::add($this->_user, $this->_post);
            if (! empty($refer))
                $this->request->redirect($refer);
            else
                $this->request->redirect('/address/list');
        }
    }

	//编辑地址
	//$nid int 地址id
    public function action_edit ($nid)
    {
        if (empty($this->_post))
        {
            $addrInfo = App_Address::detail($nid);
            $this->template->set_filename('smarty:app/address/edit');
            $this->template->set('optionsProvice', App_Region::optionsByParentId());
            $this->template->set('optionsCity', App_Region::optionsByParentId($addrInfo['proviceId']));
            $this->template->set('optionsCountry', App_Region::optionsByParentId($addrInfo['cityId']));
            $this->template->set('address', $addrInfo);
        }
        else
        {
            App_Address::edit($this->_user, $nid, $this->_post);
            $this->request->redirect('/address/list');
        }
    }
	//删除地址信息
	//$nid int 地址id
    public function action_del ($nid)
    {
        App_Address::remove($this->_user, $nid);
        $this->request->redirect('/address/list');
    }
}
